import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * @author ml
 * @create 2017-07-28--11:30
 */
public class SwingDemoMain {
    private JPanel databaseName;
    private JTextField baseLocation;
    private JTextField pfuser;
    private JButton 提交;
    private JTextField dataIp;
    private JButton 选择目录;
    private JTextField estate;
    private JTextField tomcatServer;
    private String basePath;
    private String pfuserName;
    private String estateName;
    private String dataBaseIp;
    private String serverIp;

    public SwingDemoMain() {

        提交.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                basePath=baseLocation.getText();
                basePath=basePath.replaceAll("\\\\","\\\\\\\\");
                System.out.println(basePath);
                pfuserName=pfuser.getText();
                if(pfuserName==null || "".equals(pfuserName)){
                    pfuserName="pfuser";
                }
                estateName=estate.getText();
                if(estateName==null || "".equals(estateName)){
                    pfuserName="estate";
                }
                dataBaseIp=dataIp.getText();
                serverIp=tomcatServer.getText();

                Cas cas=new Cas(basePath,pfuserName,estateName,dataBaseIp,serverIp);

                MsgService_app msgServiceApp =new MsgService_app(basePath,pfuserName,estateName,dataBaseIp,serverIp);

                MsgService_rabbit msgServiceRabbit=new MsgService_rabbit(basePath,pfuserName,estateName,dataBaseIp,serverIp);

                WFService wfService=new WFService(basePath,pfuserName,estateName,dataBaseIp,serverIp);

                Uniestate uniestate=new Uniestate(basePath,pfuserName,estateName,dataBaseIp,serverIp);

                PFFrame_flex frame_flex=new PFFrame_flex(basePath,serverIp);

                PFFrame_web frame_web=new PFFrame_web(basePath,serverIp);

                PFFrame_Pro frame_pro=new PFFrame_Pro(basePath,serverIp);
                try {
                    cas.changexml();
                    msgServiceApp.changexml();
                    msgServiceRabbit.changexml();
                    wfService.changexml();
                    uniestate.loadProperties();
                    frame_flex.changexml();
                    frame_web.changexml();
                    frame_pro.readFile();
                    JOptionPane.showMessageDialog(databaseName,"修改成功","SUCCESS",JOptionPane.INFORMATION_MESSAGE);
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(databaseName, e1.getMessage(), "ERROR",JOptionPane.ERROR_MESSAGE);
                    e1.printStackTrace();
                }


            }
        });
        选择目录.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                JFileChooser fileChooser=new JFileChooser();
                fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                fileChooser.showDialog(new Label(),"选择");
                File file=fileChooser.getSelectedFile();
                if(file!=null){

                    if(file.isDirectory()){
                        String filePath=file.getAbsolutePath();
                        System.out.println("选择的路径："+file.getAbsolutePath());
                        baseLocation.setText(filePath);
                    }
                }
            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("IP切换");
        JPanel panel=new SwingDemoMain().databaseName;
        frame.setContentPane(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setSize(500,300);
    }

}
